perf: cache Supabase admin client as module-level singleton#138
Merged
BigSimmo merged 2 commits intoJul 2, 2026
Merged
Conversation
Every API route calls createAdminClient() on every request, which previously instantiated a fresh SupabaseClient (plus requireServerEnv() + assertExpectedSupabaseProjectConfig() validation) each time. Introduce a module-level singleton using the same lazy-init pattern already used for the OpenAI client (openAIClient ??= new OpenAI(…)). The service-role client carries no user-specific session state, so reusing a single instance across requests is safe and saves per-request object construction + repeated env validation overhead. 825/825 tests pass.
Copilot created this pull request from a session on behalf of
BigSimmo
July 2, 2026 08:03
View session
…2a-45d2-8adc-9d9ded40f98a
BigSimmo
marked this pull request as ready for review
July 2, 2026 10:32
BigSimmo
enabled auto-merge
July 2, 2026 10:32
BigSimmo
deleted the
copilot/task-87357024-1243497866-93b3cae7-6f2a-45d2-8adc-9d9ded40f98a
branch
July 2, 2026 16:31
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
createAdminClient()previously constructed a freshSupabaseClient— plusrequireServerEnv()+assertExpectedSupabaseProjectConfig()— on every API request. This PR caches it as a module-level singleton, matching the existingopenAIClient ??= new OpenAI(…)pattern inopenai.ts. The service-role client holds no user-specific state (autoRefreshToken: false,persistSession: false), so sharing across requests is safe.Verification
npm run verify:cheap— 825/825 tests pass, no new type errorsnpm run verify:uiwhen UI, routing, styling, browser behavior, reduced-motion, or forced-colors behavior changednpm run verify:releasebefore release or handoff confidence claimsnpm run format:checknpm run check:production-readinesswhen clinical workflow, privacy, environment, Supabase, source governance, or deployment behavior changednpm run check:deployment-readinesswhen deployment startup, hosting, or rollout behavior changedClinical Governance Preflight
Complete this section when the change touches ingestion, answer generation, search/ranking, source rendering, document access, privacy, production env, or clinical output.
Clinical KB Database(sjrfecxgysukkwxsowpy)Notes
normalizedClinicalSearchTokensinbuildMatchExplanation(Bump actions/setup-node from 4 to 6 #2), removeexperimental.cpus: 1(Bump vitest from 4.1.6 to 4.1.7 #4), parallelisefetchRelatedDocuments(Bump gitleaks/gitleaks-action from 2 to 3 #8).